package com.tanhua.api;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.exceptionhander.CustomException;
import com.tanhua.sso.pojo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationServiceApiImpi implements UserLocationServiceApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //上报地理位置
    @Override
    public void location(Number latitude, Number longitude, String addrStr,Long userId) {
        try {
            //查看表中是有数据
            Criteria criteria = Criteria.where("userId").is(userId);
            UserLocation location = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);
            //不存在,则保存
            if (ObjectUtil.isEmpty(location)){
                UserLocation userLocation=new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setAddress(addrStr);
                userLocation.setLastUpdated(System.currentTimeMillis());
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                //longitude精度,latitude纬度
                GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude.doubleValue(), latitude.doubleValue());
                userLocation.setLocation(geoJsonPoint);
                mongoTemplate.save(userLocation);
            }else {
                //存在则更新位置和时间
                Update update=new Update();
                update.set("location",new GeoJsonPoint(longitude.doubleValue(), latitude.doubleValue())).
                        set("address",addrStr).set("updated",System.currentTimeMillis()).
                        set("lastUpdated",location.getUpdated());
                mongoTemplate.updateFirst(Query.query(criteria),update,UserLocation.class);
            }
        } catch (Exception e) {
            throw new CustomException("地理位置更新失败");
        }
    }

    //查看附近用户
    @Override
    public List<Long> selectUser(Long userId, Double distance) {
        //获取我的地理位置
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation myLocation = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);
        if (ObjectUtil.isEmpty(myLocation)){
            throw new CustomException("查不到当前用户信息");
        }
        //获取地理信息
        GeoJsonPoint location = myLocation.getLocation();
        //绘制半径
        Distance dis = new Distance(distance, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle=new Circle(location,dis);
        //查询
        List<UserLocation> locationList = mongoTemplate.find(Query.query(Criteria.where("location").withinSphere(circle).and("userId").nin(userId)), UserLocation.class);

        return locationList.stream().map(UserLocation::getUserId).collect(Collectors.toList());
    }
}
